package com.xh3140.android.widget.dialog.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.RippleDrawable;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public final class DrawableUtils {
    /**
     * 禁止实例化
     */
    private DrawableUtils() {
        throw new RuntimeException("This class prohibits instantiation.");
    }

    @Nullable
    public static Drawable android(@NonNull Context context, int resId) {
        final int[] attrs = {resId};
        final TypedArray array = context.getTheme().obtainStyledAttributes(attrs);
        final Drawable drawable = array.getDrawable(0);
        array.recycle();
        return drawable;
    }

    @NonNull
    public static ColorDrawable color(@ColorInt int color) {
        return new ColorDrawable(color);
    }

    @NonNull
    public static RippleDrawable ripple(@ColorInt int color, @ColorInt int rippleColor) {
        return new RippleDrawable(ColorStateList.valueOf(rippleColor), null, new ColorDrawable(color));
    }

    @NonNull
    public static GradientDrawable gradient(@ColorInt int color, float radius) {
        final GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(ColorStateList.valueOf(color));
        drawable.setCornerRadius(radius);
        return drawable;
    }

    @NonNull
    public static GradientDrawable gradient(@ColorInt int color, float radius, int alpha) {
        final GradientDrawable drawable = gradient(color, radius);
        drawable.setAlpha(alpha);
        return drawable;
    }

    @NonNull
    public static GradientDrawable gradient(@ColorInt int color, float radius, int strokeWidth, @ColorInt int strokeColor) {
        final GradientDrawable drawable = gradient(color, radius);
        drawable.setStroke(strokeWidth, strokeColor);
        return drawable;
    }

    @NonNull
    public static GradientDrawable divider(@ColorInt int color, int thickness) {
        final GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(ColorStateList.valueOf(color));
        drawable.setSize(thickness, thickness);
        return drawable;
    }

    @NonNull
    public static GradientDrawable divider(@ColorInt int color, int thickness, int alpha) {
        final GradientDrawable drawable = divider(color, thickness);
        drawable.setAlpha(alpha);
        return drawable;
    }
}
